//
//  HomeViewController.m
//  SHOP
//
//  Created by L on 2017/10/8.
//  Copyright © 2017年 L. All rights reserved.
//

#import "HomeViewController.h"
#import "JFLoopView.h"
#import "HomeCollectionVieModel.h"
#import "HomeCollectionViewFlowLayout.h"
#import "HomeCollectionViewCell.h"
#import "HomeNewSpaperView.h"

@interface HomeViewController () <UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)NSArray *homeCollectionArray;

@end

static NSString *homeID = @"homeCollectionCell";

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.homeCollectionArray = [self loadData];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = kTabbarTitleColor;
    [self setupUI];
}

- (void)setupUI
{
    CGRect mainViewBounds = self.navigationController.view.bounds;
    UISearchBar *customSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-120)/2), CGRectGetMinY(mainViewBounds)+22, CGRectGetWidth(mainViewBounds)-120, 40)];
    customSearchBar.delegate = self;
    customSearchBar.showsCancelButton = NO;
    customSearchBar.searchBarStyle = UISearchBarStyleMinimal;
    [self.navigationController.view addSubview: customSearchBar];
    
    //轮播
    CGFloat loopViewH = 120;
    NSArray *imageArray = @[@"积分",@"领券",@"买赠",@"三免一"];
    JFLoopView *loopView = [[JFLoopView alloc] initWithImageArray:imageArray];
    loopView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, loopViewH);
    [self.view addSubview:loopView];
    
    //项目列表
    HomeCollectionViewFlowLayout *homeFlowLayout = [[HomeCollectionViewFlowLayout alloc] init];
    UICollectionView *homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:homeFlowLayout];
    homeCollectionView.backgroundColor = [UIColor whiteColor];
    homeCollectionView.delegate = self;
    homeCollectionView.dataSource = self;
    [self.view addSubview:homeCollectionView];
    [homeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(loopView.mas_bottom).offset(0);
        make.height.offset(kScreenWidth/2);
    }];
    [homeCollectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:homeID];
    
    //品牌日
    UIImageView *dayImage = [[UIImageView alloc] init];
    dayImage.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:dayImage];
    [dayImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(homeCollectionView.mas_bottom).offset(0);
        make.height.offset(kScreenWidth/3);
    }];
    dayImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage)];
    [dayImage addGestureRecognizer:tap];

    //新闻头条
    HomeNewSpaperView *newSpaper = [[HomeNewSpaperView alloc] init];
    [self.view addSubview:newSpaper];
    [newSpaper mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(dayImage.mas_bottom).offset(0);
        make.height.offset(kScreenWidth/6);
    }];
}

#pragma mark collectionView数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.homeCollectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeID forIndexPath:indexPath];
    cell.model = self.homeCollectionArray[indexPath.row];
    return cell;
}

#pragma mark collectionView代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark 品牌日跳转
- (void)clickImage
{
    UIViewController *tableView = [[UIViewController alloc] init];
    tableView.view.backgroundColor = [UIColor yellowColor];
    [self.navigationController pushViewController:tableView animated:YES];
}

#pragma mark 加载数据
- (NSArray *)loadData
{
    NSArray *array = @[@{@"image":@"icon01",@"title":@"秒杀"},@{@"image":@"icon02",@"title":@"1元拼"},@{@"image":@"icon03",@"title":@"领卷"},@{@"image":@"icon04",@"title":@"三免一"},@{@"image":@"icon05",@"title":@"宝币"},@{@"image":@"icon06",@"title":@"买赠"},@{@"image":@"icon07",@"title":@"游戏"},@{@"image":@"icon08",@"title":@"积分"}];
    NSMutableArray *arrayM = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in array)
    {
        HomeCollectionVieModel *model = [HomeCollectionVieModel homeCollectionVieModelWithDict:dict];
        [arrayM addObject:model];
    }
    return arrayM.copy;
}

@end
